class Request {
    constructor (parms) {
        this.withBaseURL = parms.withBaseURL
        this.baseURL = parms.baseURL
    }
    get (url, data) {
        return this.request('GET', url, data)
    }
    post (url, data) {
        return this.request('POST', url, data)
    }
    put (url, data) {
        return this.request('PUT', url, data)
    }
    request (method, url, data) {
        const vm = this
        return new Promise((resolve, reject) => {
          //console.log(my.getParentAppIdSync())
            my.request({
                url: vm.withBaseURL ? vm.baseURL + url : url,
                data,
                method,
                headers: {
                    'miniAccessToken': my.getStorageSync('token'),
                    'miniAppid': my.getParentAppIdSync().appId
                },
                success (res) {
                    if (res.data.code == 7890) {
                        my.navigateTo({
                            url: "/pages/auth/openid"
                        });
                    }
                    resolve(res.data)
                },
                fail () {
                    reject({
                        msg: '请求失败',
                        url: vm.withBaseURL ? vm.baseURL + url : url,
                        method,
                        data
                    })
                }
            })
        })
    }
}

const request = new Request({
    //baseURL: 'http://192.168.3.3:50006',//https://pay.cqzykj.top//http://192.168.0.118:8666
    baseURL: 'http://192.168.0.112:8666',//http://192.168.3.3:5006https://y9z4tbq4.xiaomy.net//https://coupon.alipayjf.com//y9z4tbq4.xiaomy.net
    withBaseURL: true
})

module.exports = request
